package com.cvmars.jianyou.module.activity;

import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.cvmars.jianyou.R;
import com.cvmars.jianyou.api.api.Api;
import com.cvmars.jianyou.api.api.HttpUtils;
import com.cvmars.jianyou.api.api.SimpleSubscriber;
import com.cvmars.jianyou.api.model.HttpResult;
import com.cvmars.jianyou.api.model.UserMode;
import com.cvmars.jianyou.model.UserModel;
import com.cvmars.jianyou.module.base.BaseActivity;
import com.cvmars.jianyou.utils.ToastUtils;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.rong.imkit.RongIM;
import io.rong.imlib.model.UserInfo;

public class UpdateInfoActivity extends BaseActivity {

    @BindView(R.id.txt_info_nickName)
    EditText txtInfoNickName;
    @BindView(R.id.txt_register_btn)
    Button txtRegisterBtn;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    String from;
    @BindView(R.id.progress)
    ProgressBar progress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_info);
        ButterKnife.bind(this);

        String name = getIntent().getStringExtra("name");
        from = getIntent().getStringExtra("from");

        if (!TextUtils.isEmpty(from)) {

            if(from.equals("jineng")){
                txtTitle.setText("技能介绍");
                txtInfoNickName.setHint("请输入个人技能介绍");

            }else{

                txtTitle.setText("用户签名");
                txtInfoNickName.setHint("请输入用户签名");
            }

        }
        if (!TextUtils.isEmpty(name)) {

            txtInfoNickName.setText(name);
            txtInfoNickName.setSelection(name.length());
        }
    }

    @OnClick(R.id.txt_register_btn)
    public void onViewClicked() {


        String nickName = txtInfoNickName.getText().toString();

        if (TextUtils.isEmpty(nickName)) {

            if (from != null) {
                if ("jineng".equals(from)) {

                    ToastUtils.show("个人技能不能为空");

                }else{

                    ToastUtils.show("用户签名不能为空");
                }
            } else {

                ToastUtils.show("昵称不能为空");
            }
            return;
        }
        HashMap<String, String> map = new HashMap<>();
        if (from != null) {

            if ("jineng".equals(from)) {

                map.put("skills", nickName);

            }else{

                map.put("intro", nickName);
            }
        } else {

            map.put("name", nickName);
        }
        progress.setVisibility(View.VISIBLE);
        HttpUtils.getInstance().toSubscribe(Api.getInstance().UpdataInfoUseInfo(map)
                , new SimpleSubscriber<HttpResult<UserModel>>() {
                    @Override
                    protected void _onNext(HttpResult<UserModel> o) {
                        progress.setVisibility(View.GONE);
                        final UserModel model = o.getData();
                        RongIM.setUserInfoProvider(new RongIM.UserInfoProvider() {

                            @Override
                            public UserInfo getUserInfo(String userId) {

                                UserInfo info = new UserInfo(model.id, model.name, Uri.parse(model.avatar_url));
                                return info;
                            }
                        }, true);
                        finish();
                    }

                    @Override
                    protected void _onError(String message) {

                    }
                });

    }
}
